<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :inline="true">
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="form.orderType" filterable clearable>
          <el-option
            v-for="item in dict.type.sys_order_type"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键词" prop="keyWord">
        <el-input v-model="form.keyWord" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="form.orderNo" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item label="订单状态" prop="statusList">
        <el-select v-model="form.statusList" multiple filterable clearable>
          <el-option
            v-for="item in dict.type.ims_order_status"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单内容" prop="content">
        <el-input v-model="form.content" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item label="下单日期" prop="time">
        <el-date-picker v-model="form.time" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
                        value-format="yyyy-MM-dd" clearable></el-date-picker>
      </el-form-item>
      <el-form-item label="发货日期" prop="time2">
        <el-date-picker v-model="form.time2" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
                        value-format="yyyy-MM-dd" clearable></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" plain icon="el-icon-download"
               v-hasPermi="['ims:orderMng:export']" @click="handleExport">导出
    </el-button>
    <el-button type="success" v-hasPermi="['ims:orderMng:add']" plain icon="el-icon-check"
               @click="changeGood(0)">新增单品
    </el-button>
    <el-button type="danger" v-hasPermi="['ims:orderMng:delete']" plain icon="el-icon-check"
               @click="changeGood(1)">删除单品
    </el-button>
    <el-button type="warning" v-hasPermi="['ims:orderMng:upload']" plain icon="el-icon-check"
               @click="okImg()">图纸上唇
    </el-button>
    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleChange"
      row-key="id"
    >
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column type="index" label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderNo" label="订单号" align="center" min-width="120">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateRow(1,scope.row)">{{ scope.row.orderNo }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="orderType" label="订单类型" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="订单状态" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column prop="saleName" label="销售员" align="center" width="100"/>
      <el-table-column prop="recipient" label="收件人" align="center" width="100"/>
      <el-table-column prop="orderFrom" label="来源平台" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_source_platform" :value="scope.row.orderFrom"/>
        </template>
      </el-table-column>
      <el-table-column prop="sourceOrderNo" label="源平台单号" align="center" width="90"/>
      <el-table-column prop="content" label="订单内容" align="center" min-width="150"/>
      <el-table-column prop="expressName" label="快递公司" align="center" width="100"/>
      <el-table-column prop="expressNo" label="快递单号" align="center" width="110"/>
      <el-table-column prop="totalPrice" label="总价(元)" align="center" width="80"/>
      <el-table-column prop="urgent" label="加急单" align="center" width="90">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.urgent"/>
        </template>
      </el-table-column>
      <el-table-column prop="write" label="填单状态" align="center" width="80">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.isWrite==1">已填单</el-tag>
          <el-tag type="success" v-else>未填单</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="orderDate" label="下单日期" align="center" width="120"/>
      <el-table-column prop="sendDate" label="发货日期" align="center" width="120"/>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="updateRow(1,scope.row)"
          >详情
          </el-button>
          <el-button
            v-if="scope.row.status==10"
            v-hasPermi="['ims:orderMng:confirm']"
            type="text"
            @click="updateRow(3,scope.row)"
          >提交
          </el-button>
          <el-button
            v-hasPermi="['ims:orderMng:cancel']"
            v-if="(scope.row.orderType==10&&scope.row.status<30)||(scope.row.orderType==20&&scope.row.status<60)"
            type="text"
            @click="updateRow(4,scope.row)"
          >取消
          </el-button>
          <el-button
            v-hasPermi="['ims:orderMng:store']"
            v-if="scope.row.orderType==10&&(scope.row.status==30||scope.row.status==55||scope.row.status==40||scope.row.status==50)"
            type="text"
            @click="updateRow(5,scope.row)"
          >转库存
          </el-button>
          <el-button
            v-if="(scope.row.status==55||scope.row.status==60)&&scope.row.isWrite!=1"
            v-hasPermi="['ims:orderMng:write']"
            type="text"
            @click="updateRow(6,scope.row)"
          >填单
          </el-button>
          <el-button
            v-hasPermi="['ims:orderMng:after']"
            v-if="scope.row.status>=60&&scope.row.status<90"
            type="text"
            @click="updateRow(7,scope.row)"
          >申请售后
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="form.pageNum"
      :limit.sync="form.pageSize"
      @pagination="getList"
    />
    <addDialog ref="addDialog" @ok="handleQuery"></addDialog>
    <detailDialog ref="detailDialog" @ok="handleQuery"></detailDialog>
    <payDialog ref="payDialog" @ok="handleQuery"></payDialog>
    <afterDialog ref="afterDialog" @ok="handleQuery"></afterDialog>
    <addGoodDialog ref="addGoodDialog" @ok="handleQuery"></addGoodDialog>
    <imgUploadDialog ref="imgUploadDialog" @ok="handleQuery"></imgUploadDialog>
  </div>
</template>
<script>
import {
  listCustomOrder,
  deleteCustomOrder,
  submitCustomOrder,
  writeCustomOrder,
  changeCustomOrder
} from "../../../api/imsMng";
import addDialog from "./addDialog";
import payDialog from "./payDialog.vue";
import detailDialog from "./detailDialog.vue";
import afterDialog from "./afterDialog.vue";
import addGoodDialog from "./addGoodDialog.vue";
import {formatDay} from "../../../utils";
import imgUploadDialog from "../saleMng/saleOrder/imgUploadDialog.vue";

export default {
  name: 'OrderMng',
  components: {payDialog, detailDialog, addDialog, afterDialog, addGoodDialog, imgUploadDialog},
  dicts: ['ims_express_platform', 'ims_order_status', 'ims_source_platform', 'sys_yes_no', 'sys_order_type'],
  data() {
    return {
      tabIndex: '10',
      loading: false,
      list: [],
      form: {
        orderType: "",
        keyWord: "",
        orderNo: "",
        statusList: "",
        content: "",
        time: [],
        time2: [],
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      selectList: []
    }
  },
  created(e) {
    this.load()
  },
  methods: {
    load() {
      this.handleQuery()
    },
    handleQuery() {
      this.form.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('form')
      this.handleQuery()
    },
    handleChange(e) {
      this.selectList = e
    },
    changeGood(flag) {
      if (this.selectList.length != 1) {
        this.$message.error('请先选择一条数据')
        return
      }
      let req = {
        flag: flag,
        id: this.selectList[0].id,
        orderNo: this.selectList[0].orderNo
      }
      this.$refs.addGoodDialog.show(req)
    },
    okImg() {
      if (this.selectList.length != 1) {
        this.$message.error('请先选择一条数据')
        return
      }
      let req = {
        id: this.selectList[0].id,
        orderNo: this.selectList[0].orderNo
      }
      this.$refs.imgUploadDialog.show(req)
    },
    getList() {
      this.loading = true
      if (this.form.time && this.form.time.length) {
        this.form.startTime = this.form.time[0]
        this.form.endTime = this.form.time[1]
      } else {
        this.form.startTime = ''
        this.form.endTime = ''
      }
      if (this.form.time2 && this.form.time2.length) {
        this.form.sendStartTime = this.form.time2[0]
        this.form.sendEndTime = this.form.time2[1]
      } else {
        this.form.sendStartTime = ''
        this.form.sendEndTime = ''
      }
      listCustomOrder(this.form).then(res => {
        this.list = res.rows
        this.total = res.total
      }).finally(e => {
        this.loading = false
      })
    },
    handleExport() {
      this.download('/ims/customer/order/export', {
        ...this.form
      }, `订单列表_${formatDay(new Date())}.xlsx`)
    },
    updateRow(flag, row) {
      if (flag == 0) {
        this.$refs.addDialog.show()
      }
      if (flag == 1) {
        this.$refs.detailDialog.show(row)
      }
      if (flag == 3) {
        this.$modal.confirm('是否确定提交').then(() => {
          this.loading = true
          submitCustomOrder({id: row.id}).then(res => {
            this.$message.success('提交成功')
            this.handleQuery()
          }).finally(e => {
            this.loading = false
          })
        })
      }
      if (flag == 4) {
        this.$modal.confirm('是否确定取消').then(() => {
          deleteCustomOrder({id: row.id}).then(res => {
            this.$message.success('取消成功')
            this.handleQuery()
          })
        })
      }
      if (flag == 5) {
        this.$modal.confirm('是否确定转库存').then(() => {
          changeCustomOrder({id: row.id}).then(res => {
            this.$message.success('转库存成功')
            this.handleQuery()
          })
        })
      }
      if (flag == 6) {
        this.$modal.confirm('是否确定填写').then(() => {
          writeCustomOrder({id: row.id}).then(res => {
            this.$message.success('填写成功')
            this.handleQuery()
          })
        })
      }
      if (flag == 7) {
        this.$refs.afterDialog.show(row)
      }
    }
  }
}
</script>
